<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-row>
          <el-col :span="8">
            <el-form-item label="工作号" prop="job_number">
              <el-select v-model="form.job_number" placeholder="" clearable style="width: 100%">
                <el-option
                  v-for="(item, index) in job_number_data"
                  :key="index"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="起运时间" prop="departure_date">
              <el-date-picker v-model="form.departure_date" value-format="yyyy-MM-dd" type="date" placeholder="起运日期" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="箱型箱量" prop="box_type_volume">
              <el-input v-model="form.box_type_volume" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="起运地" prop="departure_address">
              <el-input v-model="form.departure_address" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="目的地" prop="destination">
              <el-input v-model="form.destination" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="运输公司" prop="transport_company">
              <el-input v-model="form.transport_company" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="运单状态" prop="waybill_status">
              <el-select v-model="form.waybill_status" placeholder="运单状态" clearable style="width: 100%">
                <el-option label="测试状态1" value="1" />
                <el-option label="测试状态2" value="2" />
                <el-option label="测试状态3" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态时间" prop="status_date">
              <el-date-picker v-model="form.status_date" value-format="yyyy-MM-dd" type="date" placeholder="状态时间" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车号" prop="wagon_number">
              <el-input v-model="form.wagon_number" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="车辆类型" prop="car_type">
              <el-input v-model="form.car_type" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="运输要求" prop="transport_ask">
              <el-input v-model="form.transport_ask" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="通关点地址" prop="clearance_address">
              <el-input v-model="form.clearance_address" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="司机/电话" prop="driver_telephone">
              <el-input v-model="form.driver_telephone" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="身份证号" prop="IDCard">
              <el-input v-model="form.IDCard" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="取单地址" prop="take_order_address">
              <el-input v-model="form.take_order_address" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="门店地址" prop="gate_address">
              <el-input v-model="form.gate_address" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="运单备注" prop="waybill_remarks">
              <el-input v-model="form.waybill_remarks" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位名称" prop="unit_name">
              <el-input v-model="form.unit_name" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>

      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { recordCreate, recordEdit } from '@/api/transport'
import { getJobNumber } from '@/api/business'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      activeName: 'basic',
      job_number_data: [],
      rules: {
        departure_date: [
          { required: true, message: '起运时间', trigger: 'change' }
        ],
        box_type_volume: [
          { required: true, message: '箱型箱量', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.JobNumber()
  },
  methods: {
    JobNumber() {
      getJobNumber().then(response => {
        if (response.code === 10000) {
          this.job_number_data = response.data
        }
      })
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      recordCreate(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      recordEdit(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    }
  }
}
</script>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 40%;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

